package com.mnsq.user.bean.condition;

import com.mnsq.user.bean.base.BaseCondition;
import com.mnsq.user.bean.entity.Account;
import com.vwkit.common.helper.encrypt.Base64;
import com.vwkit.common.helper.encrypt.MD5;
import com.mnsq.common.bean.annotation.JpaFields;
import com.mnsq.common.tools.valid.ValidDataParam;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.annotation.Transient;

/**
 * @author Administrator
 * @date 2019/3/18 0018
 */
@Data
@Accessors(chain = true)
public class AccountCondition extends BaseCondition<Account> {
    @JpaFields(fieldName = "userName", attrType = JpaFields.AttrType.EQ)
    private String account;
    @JpaFields(fieldName = "password", attrType = JpaFields.AttrType.EQ)
    private String password;
    @JpaFields(attrType = JpaFields.AttrType.EQ)
    private String email;
    @JpaFields(attrType = JpaFields.AttrType.EQ)
    private String mobilePhone;
    @JpaFields(fieldName = "token", attrType = JpaFields.AttrType.EQ)
    private String token;
    @JpaFields(attrType = JpaFields.AttrType.EQ)
    private String type;
    @Transient
    @ApiModelProperty(hidden = true)
    @JpaFields(attrType = JpaFields.AttrType.EQ)
    private Integer status = 1;
    @JpaFields(attrType = JpaFields.AttrType.EQ)
    private String dynamicPwd;

    @Override
    public Object getInstance() {
        return new Account();
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    /**
     * 验证数据有效性
     *
     * @return
     */
    public String valid() {
        if (StringUtils.isBlank(token) || StringUtils.isBlank(type)) {
            if (StringUtils.isBlank(account)
                    && StringUtils.isBlank(email)
                    && StringUtils.isBlank(mobilePhone)) {
                return "请输入用户名";
            } else if (StringUtils.isBlank(password) && StringUtils.isBlank(dynamicPwd)) {
                return "请输入密码";
            } else if(StringUtils.isNotBlank(account) && !ValidDataParam.wtEmail(account) && !ValidDataParam.wtMobilePhone(account)){
                return "请输入正确的手机号或邮箱";
            } else if(StringUtils.isNotBlank(this.password) && this.password.length() < 6){
                return "请输入更安全的密码";
            }
        }
        if (StringUtils.isNotBlank(this.password)) {
            this.password = MD5.get32(Base64.encryptForString(this.password));
        }
        return null;
    }
}
